package com.ruoyi.component;

import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.Dialect;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.ruoyi.common.db.DbLink;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.List;

@Component
public class AreasInitialComponent implements ApplicationRunner {
    protected final Logger logger = LoggerFactory.getLogger(AreasInitialComponent.class);
    @Value("${server.port}")
    private String port;
    @Value("${server.servlet.context-path}")
    private String path ;
    @Value("${spring.datasource.druid.master.url}")
    private String url;
    @Value("${spring.datasource.druid.master.username}")
    private String username;
    @Value("${spring.datasource.druid.master.password}")
    private String password;

    @Value("${swagger.pathMapping}")
    private String pathMapping;
    private static boolean isStart = false;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        if(isStart) {
            return ;
        }
        //启动后开始加载缓存
        try {
            startDruidPlugin();
            isStart = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public void startDruidPlugin() throws Exception {
        String ip = InetAddress.getLocalHost().getHostAddress();
        logger.info("\n----------------------------------------------------------\n\t" +
                "移动端体检预约服务启动成功! 相关地址如下:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "\n\t" +
                "Swagger-UI: \t\thttp://" + ip + ":" + port + path  +pathMapping+"swagger-ui.html\n" +
                "----------------------------------------------------------");
        //开始追加jfinal数据源
        Dialect dialect =new MysqlDialect();
        DruidPlugin druidPlugin = new DruidPlugin(url, username, password);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
//        arp.setShowSql(true);
        arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
        arp.setDialect(dialect);
        arp.setShowSql(true);
        druidPlugin.start();
        arp.start();

        logger.info("开始启动数据源中配置的数据库！");
        linkDbs();

    }


    public void linkDbs()
    {
        //获取已经启用的子数据库信息
        List<Record> list = Db.find("select * from sys_database_info where isEmploy='Y'");
        for(Record linkData:list)
        {
            try{
                DbLink.linkDb(linkData);
            }catch (Exception e)
            {
                e.printStackTrace();
                logger.error("链接["+linkData.getStr("systemname")+"]系统数据库错误！");
            }
        }
    }




}
